package com.springstudy.ex;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * @author Tamako
 * @since 2025/1/23 12:00
 */
//@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        //简单响应一个友好提示页面error1.html
        //ModelAndView modelAndView = new ModelAndView();
        //modelAndView.setViewName("/error1.html");
        //return modelAndView;

        //前后端分离开发，响应json数据
        String msg =" {\"code\": 500, \"msg\": \"服务器内部错误\"}";
        try {
            response.getWriter().write(msg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
